//
//  CheckOutViewController.m
//  at_xiaoge_client
//
//  Created by Linyu Niu on 21/03/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "CheckOutViewController.h"

@interface CheckOutViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UITableView * checkoutTableView;
    BOOL enableButton;
    NSArray * cardList;
    NSString * money;
}
@end

@implementation CheckOutViewController
@synthesize params;

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBankCard) name:@"kGetBankCardNnotification" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    __weak CheckOutViewController * weakSelf = self;
    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"提现",@"title", nil] leftAction:^(UIButton * sender){
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } rightAction:nil];
    
    checkoutTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - navView.frame.size.height - navView.frame.origin.y)];
    checkoutTableView.delegate = self;
    checkoutTableView.dataSource = self;
    checkoutTableView.backgroundColor = UIColorFromRGBWithAlpha(0x333333, 0.04);
    checkoutTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:checkoutTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
    [self getBankCard];
}

-(void)getBankCard{

}
-(void)textFieldChange:(NSNotification *)notify{
    UITextField * field = [notify object];
    if (field) {
        if ([field.text length]) {
            enableButton = YES;
        }else{
            enableButton = NO;
        }
    }
    [checkoutTableView reloadData];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
    return v;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

-(void)checkOut{
    [self.view endEditing:YES];

    [self startLoadingView:self.view];
    
    __weak CheckOutViewController * weakSelf = self;
    NSDictionary * bankData = [cardList firstObject];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath section] == 0) {
        
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    if ([indexPath section] == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(20, 20, 40, 40);

        [cell addSubview:imageView];
        
        UILabel * nameLabel = [[UILabel alloc] init];
        nameLabel.frame = CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 10, imageView.frame.origin.y, tableView.frame.size.width - imageView.frame.origin.x - imageView.frame.size.width - 20, 20);
        
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        [cell addSubview:nameLabel];
        
        
        UILabel * valueLabel = [[UILabel alloc] init];
        valueLabel.frame = CGRectMake(nameLabel.frame.origin.x , nameLabel.frame.origin.y + nameLabel.frame.size.height, nameLabel.frame.size.width , nameLabel.frame.size.height);
        
        valueLabel.font = [UIFont systemFontOfSize:14];
        valueLabel.textColor = UIColorFromRGB(0x999999);
        [cell addSubview:valueLabel];

        NSDictionary * dic = [cardList firstObject];
        nameLabel.text = [dic objectForKey:@"bankName"];
        valueLabel.text = [dic objectForKey:@"cardNO"];
        cell.cellHeight = imageView.frame.size.height + imageView.frame.origin.y * 2;
        
#if TARGET_IPHONE_SIMULATOR
        imageView.image = [UIImage imageNamed:@"default_bank_icon"];
        nameLabel.text = @"中国建设银行(0583)";
        valueLabel.text = @"储蓄卡尾号0583";
#endif
    }else if([indexPath section] == 1){
        UILabel * desLabel = [[UILabel alloc] init];
        
        desLabel.frame = CGRectMake(35, 20,tableView.frame.size.width, 20);
        desLabel.text = [NSString stringWithFormat:@"可提现 %.2f 元（最低提现100.00元）",[[params objectForKey:@"mybalance"]floatValue]];
        desLabel.font = [UIFont systemFontOfSize:14];
        desLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
        [cell addSubview:desLabel];
        
        UILabel * signalLabel = [[UILabel alloc] init];
        signalLabel.frame = CGRectMake(desLabel.frame.origin.x,desLabel.frame.origin.y + desLabel.frame.size.height + 5, 32, 32);
        signalLabel.text = @"￥";
        signalLabel.font = [UIFont systemFontOfSize:28];
        signalLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        [cell addSubview:signalLabel];
        
        UITextField * field = [[UITextField alloc] init];
        field.frame = CGRectMake(signalLabel.frame.origin.x + signalLabel.frame.size.width, signalLabel.frame.origin.y, tableView.frame.size.width - signalLabel.frame.size.width - signalLabel.frame.origin.x - 10, 32);
        field.delegate = self;
        field.keyboardType = UIKeyboardTypeNumberPad;
        field.font = [UIFont systemFontOfSize:28];
        field.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        field.text = money;
        [cell addSubview:field];
        
        
        UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(field.frame.origin.x, field.frame.origin.y + field.frame.size.height, field.frame.size.width, 1.0)];
        lineView.backgroundColor = UIColorFromRGBWithAlpha(0xE9E9E9, 1.0);
        [cell addSubview:lineView];
        
        
        UIButton * checkOutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkOutButton addTarget:self action:@selector(checkOut) forControlEvents:UIControlEventTouchUpInside];
        checkOutButton.layer.cornerRadius = 13;
        checkOutButton.frame = CGRectMake(44, lineView.frame.origin.y + lineView.frame.size.height + 20, self.view.frame.size.width - 88, 42);
        checkOutButton.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:198.0/255.0 blue:122.0/255.0 alpha:1.0];
        checkOutButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [checkOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [checkOutButton setTitle:@"2个工作日到账,确认提现" forState:UIControlStateNormal];
        [cell addSubview:checkOutButton];
        checkOutButton.enabled = enableButton;
        if (enableButton) {
            checkOutButton.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:198.0/255.0 blue:122.0/255.0 alpha:1.0];
        }else{
            checkOutButton.backgroundColor = UIColorFromRGBWithAlpha(0x333333, 0.5);
        }
        cell.cellHeight = checkOutButton.frame.size.height + checkOutButton.frame.origin.y + 10;

    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
#if TARGET_IPHONE_SIMULATOR
        return 1;
#endif
        if ([cardList count]) {
            return 1;
        }
    }else if(section == 1){
        return 1;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    money = textField.text;
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
